package top.lyc829.zzl_android.logic.util;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Looper;
import android.util.Base64;
import android.util.Log;
import android.widget.Toast;

import java.io.ByteArrayOutputStream;

import top.lyc829.zzl_android.MyApp;
import top.lyc829.zzl_android.ui.login.LoginActivity;

public final class Utils {

    public static final String TAG="myTag";
    private static final int MODE_PRIVATE = 0x0000;


    /**
     * 打log工具
     * @param log 待打印的字符串
     */
    public static void Sout(String log) {
        Log.d(TAG, log);
    }

    /**
     * 打log工具
     * @param log 待打印的字符串
     */
    public static void SoutErr(String log) {
        Log.e(TAG, log);
    }


    public static void SoutInfo(String log) {
        Log.e(TAG, log);
    }



    /**
     * 显示toast
     * @param msg 待显示的字符串
     */
    public static void showToast(String msg) {
        Toast.makeText(MyApp.getContext(), msg, Toast.LENGTH_SHORT).show();
    }

    /**
     * 显示toast的同步版本，在子线程中使用
     * @param msg 待显示的字符串
     */
    public static void showToastSync(String msg) {
        Looper.prepare();
        Toast.makeText(MyApp.getContext(), msg, Toast.LENGTH_SHORT).show();
        Looper.loop();
    }


    /**
     * 判断字符串是否为空串
     * @param str 待判断的字符串
     * @return 如果为空串则返回true，否则返回false
     */
    public static boolean isStringEmpty(String str) {
        return str == null || str.length() <= 0;
    }

    /**
     * 判断用户是否登录
     * @return
     */
    public static boolean isLogin() {
        return !isStringEmpty(getValueFromSp("token"));
    }

    public static int getUserId() {
        return isLogin() && !isStringEmpty(getValueFromSp("user_id")) ? Integer.parseInt(getValueFromSp("user_id")) : -1;
    }

    public static void goLogin(Context context) {
        Intent intent = new Intent(context, LoginActivity.class);
        context.startActivity(intent);
    }


    /**
     * 向SharedPreferences中插入键值对
     * @param key 键
     * @param val 值
     */
    public static void insertToSp(String key, String val) {
        SharedPreferences sp = MyApp.getContext().getSharedPreferences("sp_zzl", MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(key, val);
        editor.apply();
    }

    /**
     * 在SharedPreferences中根据键读取值
     * @param key 键
     * @return    对应的值，没有则返回空字符串
     */
    public static String getValueFromSp(String key) {
        SharedPreferences sp = MyApp.getContext().getSharedPreferences("sp_zzl", MODE_PRIVATE);
        return sp.getString(key, "");
    }


    public static String toBase64(Bitmap bitmap) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        byte[] bytes = baos.toByteArray();

        return Base64.encodeToString(bytes, Base64.NO_WRAP);
    }

    public static Bitmap toBitmap(Drawable drawable) {
        if (drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable) drawable).getBitmap();
        }

        int width = drawable.getIntrinsicWidth();
        width = width > 0 ? width : 1;
        int height = drawable.getIntrinsicHeight();
        height = height > 0 ? height : 1;

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);

        return bitmap;
    }

    public static Bitmap decodeResource(Context context, int resId) {
        return BitmapFactory.decodeResource(context.getResources(), resId);
    }

    public static long getCurrentTime() {
        return System.currentTimeMillis();
    }
}
